extends Node2D

@export var sand_parent:Node2D
@export var sub_view_character_node :Node2D
@export var valve: Valve
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	load_sand_map()
	await get_tree().process_frame
	await get_tree().process_frame
	CountManager.sort_sha()

func load_sand_map():
	seed(Time.get_ticks_msec())
	if is_instance_valid(sand_parent):
		CellManager.clear()
		var first_view :SubViewport = null
		for i in sand_parent.get_children():
			if not i is BaseSand:
				continue
			i.mark_rect()
			var sub_view :SubViewport= i.add_sub_viewport()
			if not is_instance_valid(first_view):
				first_view = sub_view
				var p0 = SubViewShapeParent.new()
				var p1 = SubViewShapeParent.new() 
				sub_view.add_child(p0)
				sub_view.add_child(p1)
				CellManager.player_0_shape = p0
				CellManager.player_1_shape = p1
				CellManager.sub_view = sub_view
			else:
				sub_view.world_2d = first_view.world_2d
			i.material = i.material.duplicate()
			i.material.set_shader_parameter("path_tex",sub_view.get_texture())
		CellManager.build_cells()
